package com.jxau.service.impl;

import com.jxau.entity.Article;
import com.jxau.entity.Comment;
import com.jxau.entity.User;
import com.jxau.mapper.ArticleMapper;
import com.jxau.mapper.CommentMapper;
import com.jxau.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxau.entity.Temp;
import com.jxau.service.CommentService;
import com.jxau.util.Constant;
import com.jxau.util.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Comment addCommentUnderArticle(Comment comment) {
        Date addDate = new Date();
        comment.setDate(addDate);
        //查询sender的id、用户名、头像
        String uid = comment.getSender().getId();
        User senderTem = userMapper.queryUserById(uid);
        User sender = new User();
        sender.setId(uid);
        sender.setUsername(senderTem.getUsername());
        sender.setHeadPhoto(senderTem.getHeadPhoto());
        comment.setSender(sender);
        //若为子回复，查询receiver的id、用户名、头像
        if (comment.getReceiver() != null) {
            String receiverId = comment.getReceiver().getId();
            User receiverTem = userMapper.queryUserById(receiverId);
            User receiver = new User();
            receiver.setId(receiverTem.getId());
            receiver.setUsername(receiverTem.getUsername());
            receiver.setHeadPhoto(receiverTem.getHeadPhoto());
            comment.setReceiver(receiver);
        } else {
            comment.setReceiver(new User());
        }
        commentMapper.addCommentUnderArticle(comment);
        return comment;
    }

    @Override
    public Comment addCommentReply(Comment comment) {
        Comment newComment = addCommentUnderArticle(comment);
        return newComment;
    }

    @Override
    public ArrayList<Comment> queryMyComment(String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.MY_COMMENT_PAGE_SIZE);
        ArrayList<Comment> myComments = commentMapper.queryMyComment(uid);
        for (Comment myComment : myComments) {
            myComment.setComment(HtmlUtil.parseString(myComment.getComment(), 60));
        }
        return myComments;
    }

    @Override
    public ArrayList<Comment> queryCommentOfUser(String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.MY_COMMENT_PAGE_SIZE);
        ArrayList<Comment> myComments = commentMapper.queryCommentOfUser(uid);
        for (Comment myComment : myComments) {
            User sender = userMapper.queryUserInfoById(myComment.getSenderId());
            User receiver = userMapper.queryUserInfoById(myComment.getReceiverId());
            Integer isLike = commentMapper.queryIsLike(uid, myComment.getId());
            Integer likeNum = commentMapper.queryLikeNum(myComment.getId());
            Article article = articleMapper.queryArticlesById(myComment.getArticleId());
            myComment.setArticleTitle(article.getTitle());
            myComment.setSender(sender);
            myComment.setReceiver(receiver);
            myComment.setIsLike(isLike);
            myComment.setLikeNum(likeNum);
        }
        return myComments;
    }


    @Override
    public ArrayList<Comment> queryCommentOfArticle(int articleId, String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.ARTICLE_COMMENT_PAGE_SIZE);
        return commentMapper.queryCommentOfArticle(articleId, uid);
    }


    @Override
    public List<Temp> getPages(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Temp> comments = commentMapper.getAllComments();
        return comments;
    }

    @Override
    public PageInfo<Temp> getPagesData() {
        List<Temp> comments = commentMapper.getAllComments();
        PageInfo<Temp> pageInfo = new PageInfo<>(comments);
        return pageInfo;
    }

    @Override
    public List<Temp> findCommentByName(String name) {
        return commentMapper.findCommentByName(name);
    }

    @Override
//    @Transactional
    public int deleteCommentById(int id) {
        // 删除该评论的点赞记录
//        commentMapper.deleteLikeByCommentId(id);
        // 删除该编号评论
        return commentMapper.deleteCommentById(id);
    }

    @Override
    public void cleanRedPointOfComment(String uid) {
        commentMapper.updateReadFlagOfUser(uid);
    }
}
